package org.example.exp2.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.exp2.common.CommonResponse;
import org.example.exp2.common.ResponseCode;
import org.example.exp2.entity.Book;
import org.example.exp2.entity.BookVO;
import org.example.exp2.entity.Person;
import org.example.exp2.entity.PersonVO;
import org.example.exp2.mapper.PersonMapper;
import org.example.exp2.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/persons")
@Tag(name = "图书管理API")
public class PersonController {

//    @Autowired
//    PersonService personService;
    @Autowired
    PersonMapper personMapper;

    @Value("8090")
    private int serverPort;

    public String getUrl(){
        return "http://localhost:" + serverPort;
    }


    @GetMapping("")
    @Operation(summary = "查看所有成员信息", description = "查找所有成员信息")
    public CommonResponse<Map<String,Object>> getPersons(){
        List<Person> persons = personMapper.selectList(null);
        List<PersonVO> personvo = persons.stream()
                .map(person ->{
                    PersonVO resource = new PersonVO(person.getPersonId(),person.getName(),person.getAge(),person.getEmail());
                    resource.setLinks(getUrl());
                    return  resource;
                })
                .collect(Collectors.toList());
        Map<String,Object> response = new HashMap<>();
        response.put("persons",personvo);
        response.put("total",persons.size());

        CommonResponse<Map<String,Object>> commonResponse = new CommonResponse<>(ResponseCode.SUCCESS.getCode(),"获取成员列表成功",response);
        commonResponse.getData().addLink("self",getUrl()+"/persons");

        return commonResponse;

    }


    @PostMapping("")
    @Operation(summary = "添加新成员",description = "新增一个成员到系统中")
    public CommonResponse<Map<String,Object>> addPerson(@Parameter(description = "人员对象",required = true)@RequestBody Person person){
        int result=personMapper.insert(person);
        if(result >0){
            Map<String,Object> response = new HashMap<>();
            response.put("personId",person.getPersonId());

            CommonResponse<Map<String,Object>> commonResponse = new CommonResponse<>(ResponseCode.SUCCESS.getCode(), "新成员添加成功", response);
            commonResponse.getData().addLink("self", getUrl() + "/persons/" + person.getPersonId());
            return commonResponse;
        }else {
            return new CommonResponse<>(ResponseCode.ERROR.getCode(),"新成员添加失败");
        }
    }

    @PutMapping("/personId")
    @Operation(summary = "更新成员信息",description = "根据personId的更新成员信息")
    public CommonResponse<Void> updatePerson(@Parameter(description = "成员ID",required = true)@PathVariable("personId") int personId,
                                             @Parameter(description = "成员对象",required = true) @RequestBody Person perosn){
        perosn.setPersonId(personId);
        int result=personMapper.updateById(perosn);
        if (result>0){
            CommonResponse<Void> commonResponse = new CommonResponse<>(ResponseCode.SUCCESS.getCode(),"成员信息更新成功");
            commonResponse.getData().addLink("self",getUrl()+"/persons/"+perosn.getPersonId());

            return commonResponse;
        }else {
            return new CommonResponse<>(ResponseCode.ERROR.getCode(),"成员信息更新失败");
        }
    }

    @DeleteMapping("/{personId}")
    @Operation(summary = "删除图书",description = "根据成员ID从系统中删除成员")
    public CommonResponse<Void> deletePerson(@Parameter(description = "personId",required = true)
                                             @PathVariable("personId") int personId){
        int result = personMapper.deleteById(personId);
        if(result>0){
            CommonResponse<Void> commonResponse = new CommonResponse<>(ResponseCode.SUCCESS.getCode(),"成员删除成功");
            commonResponse.getData().addLink("self",getUrl()+"/persons");

            return commonResponse;
        }else {
            return new CommonResponse<>(ResponseCode.ERROR.getCode(),"成员删除失败");
        }
    }
}
